package com.hu.zhcc.shiro.service.xsf.impl;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.xsf.TMealsMapper;
import com.hu.zhcc.shiro.entity.xsf.dto.THealthPlanDTO;
import com.hu.zhcc.shiro.entity.xsf.dto.TMealsDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.THealthPlan;
import com.hu.zhcc.shiro.entity.xsf.pojo.TMeals;
import com.hu.zhcc.shiro.service.xsf.TMealsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class TMealsImpl implements TMealsService {
    @Autowired
    private TMealsMapper tMealsMapper;

    @Override
    public Page<TMealsDTO> queryAllTMeals(Map<String, Object> paremeters, int offset, int limit) {
        int total = this.tMealsMapper.countTMeals(paremeters);
        List<TMealsDTO> dtoList = null;
        if(total > 0) {
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<TMeals> list = this.tMealsMapper.queryAllTMeals(pageRequest);
            dtoList = new ArrayList<TMealsDTO>(list.size());
            for(TMeals tMeals : list) {
                TMealsDTO dto = new TMealsDTO();
                BeanUtils.copyProperties(tMeals, dto);
                dtoList.add(dto);
            }
        }
        return new Page<TMealsDTO>(total, dtoList);
    }

    @Override
    public TMealsDTO getById(int id) {
        TMeals tMeals = this.tMealsMapper.selectByPrimaryKey(id);
        if(tMeals == null) {
            return null;
        }
        TMealsDTO dto = new TMealsDTO();
        BeanUtils.copyProperties(tMeals, dto);
        return dto;
    }

    @Override
    public TMealsDTO saveTMeals(TMealsDTO dto) {
        TMeals tMeals = new TMeals();
        BeanUtils.copyProperties(dto, tMeals);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        tMeals.setCreateTime(simpleDateFormat.format(new Date()));
        this.tMealsMapper.insertSelective(tMeals);
        dto.setId(tMeals.getId());
        return dto;
    }

    @Override
    public int removeTMeals(int id) {
        return tMealsMapper.deleteByPrimaryKey(id);

    }

    @Override
    public int updateTMeals(TMeals tMeals) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        tMeals.setUpdateTime(simpleDateFormat.format(new Date()));
        return tMealsMapper.updateByPrimaryKeySelective(tMeals);

    }


}
